package org.movee.spring.data.hugegraph.gremlin;


import lombok.NonNull;

public class GremlinTraversal {

    // private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private String traversal;

    private GremlinTraversal(final String traversal) {
        this.traversal = traversal;
    }

    public String getTraversal() {
        return traversal;
    }

    public static GremlinTraversal createDirectly(final String script) {
        return new GremlinTraversal(script);
    }

    public static Builder builder() {
        return new Builder();
    }

    public static Builder builder(final String graph) {
        return new Builder(graph);
    }


    public static class Builder {

        // private final Logger logger = LoggerFactory.getLogger(this.getClass());

        private final StringBuilder sb = new StringBuilder();

        Builder() { }

        Builder(final String graph) {
            sb.append(graph);
        }

        public GremlinTraversal build() {
            return new GremlinTraversal(sb.toString());
        }

        public Builder stepQuote(final String format, final Object... args) {
            String[] s = new String[args.length];
            for (int i = 0; i < args.length; i ++) {
                s[i] = GremlinUtils.quoteIfNeeded(args[i]);
            }

            sb.append(String.format(format, s));

            return this;
        }

        public Builder step(final String format, final Object... args) {
            sb.append(String.format(format, args));
            return this;
        }

        public Builder stepProperty(@NonNull String key, Object value) {
            if (value != null) {
                sb.append(String.format(".property('%s', %s)", key, GremlinUtils.quoteIfNeeded(value)));
            }
            return this;
        }

    }


}
